package com.mp.chart.charts;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.mp.chart.components.AJXAxis;
import com.mp.chart.components.AJYAxis;
import com.mp.chart.components.MarkerView;
import com.mp.chart.components.YAxis;
import com.mp.chart.data.Entry;
import com.mp.chart.interfaces.AJProvider;
import com.mp.chart.listener.AJChartTouchListener;
import com.mp.chart.listener.OnScrollDataListener;
import com.mp.chart.listener.OnSyncChartListener;
import com.mp.chart.renderer.AJBarChartRenderer;
import com.mp.chart.renderer.AJXAxisRenderer;
import com.mp.chart.renderer.AJYAxisRenderer;
import com.mp.chart.utils.Highlight;

/*重写柱状图*/
public class MinutesBarChart extends BarChart implements AJProvider {

    protected boolean drawbarEnable;
    protected AJChartTouchListener mMarketTouchListener;
    private OnScrollDataListener mOnScrollDataListener;
    private MarkerView mXAxisMarkerView;

    public MinutesBarChart(Context context) {
        super(context);
    }

    public MinutesBarChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MinutesBarChart(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void init() {
        super.init();
        setDescription("");
        mAxisLeft = new AJYAxis(YAxis.AxisDependency.LEFT);
        mAxisRendererLeft = new AJYAxisRenderer(mViewPortHandler, (AJYAxis) mAxisLeft, mLeftAxisTransformer);
        mXAxis = new AJXAxis();
        mXAxisRenderer = new AJXAxisRenderer(this, mViewPortHandler, (AJXAxis) mXAxis, mLeftAxisTransformer);
        mRenderer = new AJBarChartRenderer(this, mAnimator, mViewPortHandler);//柱状图高亮和数据样式重写
        mChartTouchListener = new AJChartTouchListener(this, mViewPortHandler.getMatrixTouch());

        mMarketTouchListener = (AJChartTouchListener) mChartTouchListener;
    }

    public void setLongPressEnable(boolean value) {
        mMarketTouchListener.setLongPressEnable(value);
    }

    @Override
    public AJYAxis getAxisLeft() {
        return (AJYAxis) super.getAxisLeft();
    }

    public void setDrawBarEnable(boolean enable) {
        this.drawbarEnable = enable;
    }

    public AJXAxis getXAxis() {
        return (AJXAxis) mXAxis;
    }

    @Override
    protected void calcMinMax() {
        super.calcMinMax();

        if (!drawbarEnable) {
            mXChartMin = 0;
            mXChartMax = mData.getXVals().size() - 1;
            mDeltaX = Math.abs(mXChartMax - mXChartMin);
        }
    }

    @Override
    protected void calcModulus() {

        mXAxis.mAxisLabelModulus = 1;
    }

//    private float[] getMatrixValues(Matrix matrix) {
//        float[] val = new float[9];
//        matrix.getValues(val);
//        return val;
//    }

    public void syncMatrix(float x, float sx, boolean islimit) {
//        mMarketTouchListener.prepareSync();
//        Matrix matrix = mViewPortHandler.getMatrixTouch();
//        float[] val = getMatrixValues(matrix);
//
//        val[Matrix.MTRANS_X] = x;
//        val[Matrix.MSCALE_X] = sx;
//
//        matrix.setValues(val);
//
//        if (!islimit) {
//            invalidate();
//        } else {
//            mViewPortHandler.refresh(matrix, this, true);
//        }
    }

    @Override
    public void setOnLoadingViewListener(AJChartTouchListener.OnLoadingViewListener l) {
        mMarketTouchListener.setOnLoadingViewListener(l);
    }


    @Override
    public OnScrollDataListener getOnScrollDataListener() {
        return mOnScrollDataListener;
    }

    @Override
    public void setOnScrollDataListener(OnScrollDataListener l) {
        this.mOnScrollDataListener = l;
    }

    public void setOnSyncChartListener(OnSyncChartListener l) {
        mMarketTouchListener.setOnSyncChartListener(l);
    }

    @Override
    public void computeScroll() {

        mMarketTouchListener.computeScroll();
    }

//    public void closeLoadingView() {
//        mMarketTouchListener.closeLoading();
//
//    }

    @Override
    public boolean getDrawbarEnable() {
        return drawbarEnable;
    }

    @Override
    public boolean getHighLightStyle() {
        return false;
    }

    int select = 0;

    public Entry highlightTouchWithoutEvent(Highlight high, int isSelect) {
        this.select = isSelect;
        Entry e = null;

        if (high == null) {
            if (select == 1) {
                mIndicesToHightlight = new Highlight[]{new Highlight(mData.getXValCount(), 0)};
            } else if (select == 0) {
                mIndicesToHightlight = null;
            }
        } else {

            if (mLogEnabled)
                Log.i(LOG_TAG, "Highlighted: " + high.toString());

            e = mData.getEntryForHighlight(high);
            if (e == null ) {
                mIndicesToHightlight = null;
                high = null;
            } else {
                // set the indices to highlight
                mIndicesToHightlight = new Highlight[]{
                        high
                };
            }
        }

        // redraw the com.mp.chart
        invalidate();
        return e;
    }

    /*bar marker位置设置*/
    protected void drawMarkers(Canvas canvas) {

        // if there is no marker view or drawing marker is disabled

        if (mXAxisMarkerView == null) {
            return;
        }

        if (!mDrawMarkerViews || !valuesToHighlight())
            return;

        for (int i = 0; i < mIndicesToHightlight.length; i++) {

            int xIndex = mIndicesToHightlight[i].getXIndex();
            int dataSetIndex = mIndicesToHightlight[i].getDataSetIndex();

            if (xIndex <= mDeltaX && xIndex <= mDeltaX * mAnimator.getPhaseX()) {

                Entry e = mData.getEntryForHighlight(mIndicesToHightlight[i]);

                // make sure entry not null
                if (e == null || e.getXIndex() != mIndicesToHightlight[i].getXIndex())
                    continue;

                float[] pos = getMarkerPosition(e, dataSetIndex);


                if (mXAxisMarkerView != null) {
                    measureMarkerView(mXAxisMarkerView, dataSetIndex, e);
                    float x = pos[0];
                    float y1 = mViewPortHandler.contentBottom();
                    float y2 = mViewPortHandler.contentTop();
                    if (pos[0] + mXAxisMarkerView.getXOffset() < mViewPortHandler.contentLeft()) {
                        x = mViewPortHandler.contentLeft() - mXAxisMarkerView.getXOffset();
                    } else if (pos[0] - mXAxisMarkerView.getXOffset() > mViewPortHandler.contentRight()) {
                        x = mViewPortHandler.contentRight() + mXAxisMarkerView.getXOffset();
                    } else {
                        x = pos[0];
                    }

                    mXAxisMarkerView.draw(canvas, x, y2);

                }
            }
        }
    }

    private void measureMarkerView(MarkerView markerView, int dataSetIndex, Entry e) {
        markerView.refreshContent(e, dataSetIndex);
        markerView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        markerView.layout(0, 0, markerView.getMeasuredWidth(),
                markerView.getMeasuredHeight());
    }

    public void setXAxisMarkerView(MarkerView leftMarkerView) {
        this.mXAxisMarkerView = leftMarkerView;
    }
}